# -- coding:utf-8 --

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QLabel
from ScreenPoint import *
from Ultrasonic import ultralsonic
from RFIDCard import RFIDRead
import signal
import time

class ClearThread(QtCore.QThread):    #延时清除线程，超时发出清屏信号
    ClearPoint = QtCore.pyqtSignal()    #清屏信号

    def __init__(self, parent = None):
        super(ClearThread, self).__init__(parent)
        self.num = 0

    def run(self):
        for i in range(5):    #计数5次即计数5秒
            time.sleep(1)
            self.num += 1
            if self.num >= 5:
                self.ClearPoint.emit()    #满5秒清屏

class Screen(QtGui.QMainWindow):  #PyQt主窗口类，继承于QtGui.QMainWindow类

    def __init__(self):  #初始化类
        super(Screen, self).__init__()

        self.TouchPoint = ScreenPoint(0x1ff7,0x0013)    #屏幕类        

        self.resize(1080,1500)  #窗口大小
        self.move(0,390)  #窗口位置
        self.setWindowTitle('Brushes')  #窗口标题
        self.ButtonSet()    #设置按钮
        self.LabelSet()

        screensize = QtGui.QDesktopWidget().screenGeometry()
        print screensize.width()
        print screensize.height()

        self.TouchPoint.pixx = screensize.width()
        self.TouchPoint.pixy = screensize.height()
        self.PointDraw = PointThread(self.TouchPoint)    #调用获取屏幕触点的线程
        self.PointDraw.Point.connect(self.DrawPoint)    #将触点信号连接至DrawPoint方法，即有信号发出时，该方法被调用

        self.DistanceThread = ultralsonic(9600)
        self.DistanceThread.RangeSignal.connect(self.ShowDistance)
        self.Distance = 0

        self.CardID = RFIDRead()
        self.CardID.IDSignal.connect(self.RFIDAct)

        self.ClearPoint = ClearThread()
        self.ClearPoint.ClearPoint.connect(self.ClearPointClick)
        # self.ClearPoint.start()

        file = QtCore.QFile("MyStyle.qss")
        file.open(QtCore.QFile.ReadOnly)
        styleSheet = file.readAll()
        styleSheet = unicode(styleSheet, encoding='utf8')
        QtGui.qApp.setStyleSheet(styleSheet)

        self.showFullScreen()

        self.circlesize = 20  #绘制点的大小
        # self.count = 0  #计数绘制的点数
        self.pix = []    #pix坐标
        self.mm = []    #mm坐标
        self.ymm = []

    def DrawPoint(self, isdata, ThePoint):    #由触点线程中的触点信号触发，point为传递进来的参数
        if isdata == True:
            self.ClearPoint.num = 0
            if ThePoint[0:2] not in self.mm:
                if (ThePoint[2] != 1080 | ThePoint[3] != 0):
                    self.pix.append(ThePoint[2:4])
                    self.mm.append(ThePoint[0:2])
                    self.update()
                    print (ThePoint)
                    self.ymm.append(ThePoint[1])
                    Score = max(self.ymm)
                    self.TouchContentLabel.setText(str(Score)+"mm")
                    self.ScoreContentLabel.setText(str(Score + self.Distance)+"mm")
        else:
            self.ClearPoint.start()
 
    def ShowDistance(self, Distance):
        self.Distance = int(Distance)
        self.DistanceContentLabel.setText(str(self.Distance) + "mm")

    def RFIDAct(self, IDNum):
        self.IDContentLabel.setText(IDNum)
        self.DistanceThread.start()

    def closeEvent(self, event):    #重写窗口关闭事件，窗口关闭时触发
        self.statusBar().showMessage('quiting...')    #显示一个状态栏
        reply = QtGui.QMessageBox.question(self, 'Message', 'quit?',
                                            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
                                            QtGui.QMessageBox.No)    #确认对话框
        if reply == QtGui.QMessageBox.Yes:    #确认退出
            self.PointDraw.StartFlag = False    #停止线程，run函数检测到标志则退出
            self.TouchPoint.release()    #释放屏幕占用
            print "程序退出"    #打印一条信息
            event.accept()    #响应该退出事件
        else:     #不确认退出
            self.statusBar().showMessage('')    #显示一个状态栏
            event.ignore()    #不响应该退出事件

    def ButtonSet(self):
        ClearButton = QtGui.QPushButton("clear", self)  #创建一个清除按钮
        ClearButton.move (10,10)  #按钮位置
        ClearButton.resize(75,45)  #按钮大小
        self.connect(ClearButton, QtCore.SIGNAL('clicked()'), self.ClearPointClick)  #关联按钮事件触发函数为ClearButtonClick

        StartButton = QtGui.QPushButton("Start",self)    #创建一个开始按钮
        StartButton.move(135,10)    #按钮位置
        StartButton.resize(75,45)    #按钮大小
        self.connect(StartButton, QtCore.SIGNAL('clicked()'), self.StartButtonClick)    #关联按钮事件触发函数为StartButtonClick

    def LabelSet(self):
        IDLabel = QLabel(self)
        IDLabel.resize(65, 40)
        IDLabel.move(10, 80)
        IDLabel.setText(u"卡号:")

        self.IDContentLabel = QLabel(self)
        self.IDContentLabel.resize(135, 40)
        self.IDContentLabel.move(75, 80)
        self.IDContentLabel.setText("")

        TouchLabel = QLabel(self)
        TouchLabel.resize(65, 40)
        TouchLabel.move(10, 145)
        TouchLabel.setText(u"触点:")

        self.TouchContentLabel = QLabel(self)
        self.TouchContentLabel.resize(135, 40)
        self.TouchContentLabel.move(75, 145)
        self.TouchContentLabel.setText("")

        DistanceLabel = QLabel(self)
        DistanceLabel.resize(65, 40)
        DistanceLabel.move(10, 210)
        DistanceLabel.setText(u"高度:")

        self.DistanceContentLabel = QLabel(self)
        self.DistanceContentLabel.resize(135, 40)
        self.DistanceContentLabel.move(75, 210)
        self.DistanceContentLabel.setText("")

        ScoreLabel = QLabel(self)
        ScoreLabel.resize(65, 40)
        ScoreLabel.move(10, 275)
        ScoreLabel.setText(u"成绩:")

        self.ScoreContentLabel = QLabel(self)
        self.ScoreContentLabel.resize(135, 40)
        self.ScoreContentLabel.move(75, 275)
        self.ScoreContentLabel.setText("")

    def ClearPointClick(self):  #响应按钮点击事件
        self.pix = []
        self.mm = []
        self.ymm = []
        self.TouchContentLabel.setText("")
        self.ScoreContentLabel.setText("")
        # self.count = 0  #计数清零
        self.update()  #重绘界面

    def StartButtonClick(self):
        self.TouchPoint.connect()
        self.PointDraw.StartFlag = True
        self.PointDraw.start()    #开始线程，run方法被调用
        self.DistanceThread.start()
        self.CardID.start()

    def paintEvent(self,e):  #重绘界面事件发生时，该函数强制运行执行重绘界面操作
        qp = QtGui.QPainter()  #Qt画笔
        qp.begin(self)  #开始绘制
        self.drawBrushes(qp)  #绘制函数
        qp.end()  #结束绘制

    def keyPressEvent(self, e):
        if e.key() == QtCore.Qt.Key_Escape:
            self.close()

    def drawBrushes(self, qp):
        #self.count = self.count + 1
        a = len(self.pix)
        print (a)
        brush = QtGui.QBrush(QtCore.Qt.SolidPattern)  #实心画笔
        qp.setBrush(brush)  #设置画笔
        for i in range(a):
            qp.drawEllipse(self.pix[i][0], self.pix[i][1] - self.y() - 30, self.circlesize, self.circlesize)  #绘制每个点

def KeyQuit(signal,frame):
    ex.TouchPoint.release()
    print "keyboard quit!"
    sys.exit(app.exec_())

signal.signal(signal.SIGINT,KeyQuit)

app = QtGui.QApplication(sys.argv)    #创建应用
ex = Screen()
ex.show()
sys.exit(app.exec_())
